package org.example.message_queue.mqserver.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/*
*交换机
*/
public class Exchange {
    private String name;
    private ExchangeType type = ExchangeType.DIRECT;
//    交换机是否需要持久化
    private boolean durable = false;
//    自动删除
    private boolean autoDelete = false;
//    创建交换机时指定的一些额外参数选项
    private Map<String,Object> arguments = new HashMap<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ExchangeType getType() {
        return type;
    }

    public void setType(ExchangeType type) {
        this.type = type;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    public void setArguments(Map<String,Object> arguments) {
        this.arguments = arguments;
    }

//    public Map<String, Object> getArguments() {
//        return arguments;
//    }
    public String getArguments(){
//        将Map转化为String（Json）
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
//        如果真的异常了，返回一个空的Jason字符串
        return "{}";
    }

//    public void setArguments(Map<String, Object> arguments) {
//        this.arguments = arguments;
//    }
    public void setArguments(String argumentsJson){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
           this.arguments = objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String,Object>>() {});
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

//    测试的时候使用
    public Object getArguments(String key){
        return arguments.get(key);
    }


    public void setArguments(String key, Object value){
        arguments.put(key, value);
    }
}
